var stream = {
	isOpenUserMedia: false,
	mimeCodec: 'video/webm; codecs=vp9,opus',
	isRecorder: false,
	time: 60,
	topic: null,
	number: 0,
	media: function(facingMode, v, topic) {
		stream.topic = topic;
		// 调用用户媒体设备
		if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia
			|| navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
			// 访问摄像头或麦克风 ,facingMode:{exact:facingMode}
			//设置摄像头最低分辨率，低于该分辨率降无法唤起摄像头，quality代表视频质量
			//var videoConfig = { width: { min: 1920 }, height: { min: 1080 }};
			//var videoConfig = { width: { min: 1280 }, height: { min: 720 }};
			var videoConfig = { width: { min: 848 }, height: { min: 480 }};
			if (facingMode != "") {
				videoConfig.facingMode = { "exact": facingMode };
			}
			stream.getUserMedia({
				video: videoConfig,
				audio: true
			}, function(MediaStream) {
				v.srcObject = MediaStream;
				v.play();
				stream.mediaStream = MediaStream;
				stream.createMediaRecorder(stream.topic);
				// 订阅目标的消息
				websocket.subscribe(stream.topic + "-toCollection", function(msg) {
					var data = JSON.parse(msg.body);
					if (data.command == "createMediaRecorder") {
						if (stream.isRecorder) {
							stream.isRecorder = false;
							// 状态为recording时才进行停止
							if(stream.mediaRecorder.state == 'recording'){
								stream.mediaRecorder.stop();
							}
						}
						stream.createMediaRecorder(stream.topic);
					}
				});
			}, function(err) {
				console.log(err);
			});
		}
	},
	// 推流
	createMediaRecorder: function(topic) {
		var options = {
			//audioBitsPerSecond : 128000,
			videoBitsPerSecond: 407040, // 848 x 480
			//videoBitsPerSecond: 921600, // 1280 x 720
			//videoBitsPerSecond: 1105920,
			mimeType: stream.mimeCodec
		}
		websocket.send(topic + "-toShow", JSON.stringify({ "command": "createMediaSource" }));
		stream.mediaRecorder = new MediaRecorder(stream.mediaStream, options);
		stream.isRecorder = true;
		setTimeout(function() {
			stream.number = 0;
			// 状态为inactive才进行启动
			if(stream.mediaRecorder.state == 'inactive'){
				stream.mediaRecorder.start(stream.time);
			}
		}, 30);
		stream.mediaRecorder.ondataavailable = function(e) {
			// 将内存里面的视频流数据转成ArrayBuffer
			var reader = new FileReader();
			reader.onloadend = function() {
				if (reader.result.byteLength > 0) {
					var buf = new Uint8Array(reader.result);
					// 发送视频流信息
					if (stream.isRecorder) {
						websocket.send(topic, buf, true, { "number": stream.number });
						stream.number++;
					}
				}
				reader = null;
			}
			reader.readAsArrayBuffer(e.data);
		}
	},
	// 访问用户媒体设备的兼容方法
	getUserMedia: function(constraints, success, error) {
		if (navigator.mediaDevices.getUserMedia) {
			// 最新标准API
			navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
		} else if (navigator.webkitGetUserMedia) {
			// webkit内核浏览器
			navigator.webkitGetUserMedia(constraints, success, error);
		} else if (navigator.mozGetUserMedia) {
			// Firefox浏览器
			navigator.mozGetUserMedia(constraints).then(success).catch(error);
		} else if (navigator.getUserMedia) {
			// 旧版API
			navigator.getUserMedia(constraints).then(success).catch(error);
		}
	}
}